package reflect;

import java.io.File;
import java.net.URISyntaxException;

/**
 * @author DBX
 * @effect 反射机制中获取注解信息
 * @date 2021/09/16/9:31
 */
public class ReflectDemo11 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, IllegalAccessException, InstantiationException {
//        自动实例化reflect中有@AutoRunClass标注的信息
        File dir = new File(ReflectDemo11.class.getResource(".").toURI());
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub : subs) {
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.indexOf("."));

            Class cls = Class.forName("reflect."+className);
//            System.out.println("获取类名："+cls);
            if (cls.isAnnotationPresent(AutoRunClass.class)){
                System.out.println(className+"被标注了");
                Object obj = cls.newInstance();
                System.out.println(obj);
            }
        }
    }
}
